
/**
 * 数字加减框
 */
Component({
	/**
	 * 组件的属性列表
	 */
	options: {
	},
	properties: {
		fontsize: String,
		height: String,
		increase: Boolean,
		max: Number,
		min: Number,
		num: Number,
		reduce: Boolean,
		wid: String,
		width: String
	},

	attached() {
		const _t = this;
		const num = Math.abs(~~Number(_t.data.num));
		_t.setData({
			increase: num < _t.data.max ? true : false,
			num,
			reduce: num > _t.data.min ? true : false
		});
	},

	methods: {
		input(e: any) {
			const v = Number(e.detail.value);
			if (this.data.min <= v && v <= this.data.max) {
				this.render(v);
			} else if (v < this.data.min) {
				this.render(this.data.min);
			} else {
				this.render(this.data.max);
			}
		},
		increase() {
			const _t = this;
			this.render(_t.data.num + 1);
		},
		reduce() {
			const _t = this;
			this.render(_t.data.num - 1);
		},
		get_num() {
			return this.data.num;
		},
		render(num: number) {
			const obj = {} as { reduce: boolean, increase: boolean, num: number };
			if (num <= this.data.min) {
				obj.reduce = false;
			} else {
				obj.reduce = true;
			}
			if (num >= this.data.max) {
				obj.increase = false;
			} else {
				obj.increase = true;
			}
			obj.num = Number(num);
			this.setData(obj);
			this.triggerEvent('fdwe-input', { max: this.data.max, min: this.data.min, num: this.data.num, wid: this.data.wid }, { bubbles: true });
		}
	}
});
